<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-tabs>
        <!-- 放置页签 -->
        <el-tab-pane label="角色管理">
          <!-- 角色管理区域 -->
          <el-row style="height:60px">
            <el-button
              icon="el-icon-plus"
              size="small"
              type="primary"
              @click="$refs.addRole.dialogShow = true"
            >新增角色
            </el-button>
          </el-row>
          <!-- 表格 -->
          <el-table border stripe :data="tableData" style="width: 100%">
            <el-table-column type="index" label="序号" width="120"/>
            <el-table-column prop="name" label="角色名称" width="240"/>
            <el-table-column prop="description" label="角色描述" width="240"/>
            <el-table-column v-slot="scope" label="操作">
              <el-button size="small" type="primary" @click="openDialog(scope.row.id)">分配权限</el-button>
              <el-button size="small" type="success" @click="$refs.addRole.getRoleInfo(scope.row.id)">编辑</el-button>
              <el-button size="small" type="danger" @click="delRole(scope.row.id)">删除</el-button>
            </el-table-column>
          </el-table>
          <!-- 分页组件 -->
          <el-pagination
            background
            :page-sizes="[10, 20, 40, 80]"
            :page-size="queryData.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            :current-page.sync="queryData.page"
            @size-change="sizeChange"
            @current-change="pageChange"
          />
        </el-tab-pane>
        <el-tab-pane label="公司信息">
          <!--公司信息-->
          <el-alert
            title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
            type="info"
            show-icon
            :closable="false"
          />
          <el-form label-width="120px" style="margin-top:50px">
            <el-form-item label="公司名称">
              <el-input
                v-model="companyInfo.name"
                disabled
                style="width:400px"
              />
            </el-form-item>
            <el-form-item label="公司地址">
              <el-input
                v-model="companyInfo.companyAddress"
                disabled
                style="width:400px"
              />
            </el-form-item>
            <el-form-item label="邮箱">
              <el-input
                v-model="companyInfo.mailbox"
                disabled
                style="width:400px"
              />
            </el-form-item>
            <el-form-item label="备注">
              <el-input
                v-model="companyInfo.remarks"
                type="textarea"
                :rows="3"
                disabled
                style="width:400px"
              />
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
    </div>
    <!--新增面板-->
    <add-role ref="addRole" @updateList="initData"/>
    <!--编辑权限面板-->
    <set-auth ref="setAuth" @updateList="initData"/>
  </div>
</template>

<script>
import { deleteRoleApi, getCompanyInfoApi, getRoleListApi } from '@/api/setting'
import { mapState } from 'vuex'
import SetAuth from '@/views/settings/components/setAuth.vue'

export default {
  name: 'Settings',
  components: {
    SetAuth,
    AddRole: () => import('./components/addRole')
  },
  data() {
    return {
      tableData: [],
      queryData: {
        page: 1,
        pagesize: 10
      },
      // 列表总数
      total: 0,
      // 公司信息
      companyInfo: {}
    }
  },
  computed: {
    ...mapState('user', ['userInfo'])
  },
  created() {
    this.initData()
    this.getCompanyData()
  },
  methods: {
    async initData() {
      const { rows, total } = await getRoleListApi(this.queryData)
      this.tableData = rows
      this.total = total
    },
    async getCompanyData() {
      // 获取公司信息
      this.companyInfo = await getCompanyInfoApi(this.userInfo.companyId)
    },
    // 删除事件
    async delRole(id) {
      try {
        await this.$confirm('此操作将会删除该角色, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
      } catch (error) {
        return console.log(error)
      }
      // 发送删除请求
      await deleteRoleApi(id)
      // 提示成功
      this.$message.success('删除成功')
      // 边缘问题 当删除最后一页的最后一条数据时，需要重新请求数据页码减一
      if (this.queryData.page > 1 && this.tableData.length === 1) {
        this.queryData.page--
      }
      // 重新获取数据
      await this.initData()
    },
    // 每页数量切换事件
    sizeChange(val) {
      this.queryData.pagesize = val
      this.initData()
    },
    // 页码切换事件
    pageChange(val) {
      this.queryData.page = val
      this.initData()
    },
    // 设置权限窗口
    openDialog(id) {
      // 获取所有权限
      this.$refs.setAuth.getAllAuthList()
      // 获取当前角色信息
      this.$refs.setAuth.getCurrentRoleInfo(id)
      // 打开弹窗
      this.$refs.setAuth.dialogShow = true
    }
  }
}

</script>

<style scoped>

</style>
